import json
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render, render_to_response
from django.template import RequestContext
from django.views.decorators.csrf import csrf_exempt

from GoodTools.ToolsControl import weather, index as _index, pathname
from UserControl.useControl.login import _Login
from UserControl.useControl.register import _Register


from DataDisplay.DisplayControl import area, salary, area_map, undefined_4
from PolicyData.PolicyControl import policy
from DataDisplay.DisplayControl import analysis
from Common import Fession
import time
# Create your views here.


#主页
def index(request):
    #Fession.Fession().getFassion("indexCount")
    return render_to_response('index.html', {'Meg':_index._index().main()})
def returnFun(request):
    if request.method == "POST":
        # 获取天气
        weather_mag = weather._Weather().get_weather()
        print('a test of weather_mag:', weather_mag)
        return HttpResponse(json.dumps(weather_mag))
    elif request.method == "GET":
        #LoginName_session = request.session.get('LoginName_session', 'anybody')
        LoginName_session = request.session.get('LoginName_session',None)
        print('成功还是毁灭', LoginName_session)
        print(json.dumps(LoginName_session))
        return HttpResponse(json.dumps(LoginName_session))
def returnPath(request):
    if request.method == "GET":
        path_js = request.GET.get('path_js')
        print(path_js)
        path_list = path_js.split('/')
        print(path_list)
        path_dict = pathname._pathname().get_path(path_list)
        return HttpResponse(json.dumps(path_dict))
def login_html(request):
    # 创建或修改 session：
    request.session['name'] = "fang"
    # 获取 session：
    name = request.session.get('name', "hh")
    print(name)
    # # 删除 session
    # del request.session["name"]  # 不存在时报错
    return render_to_response('login.html')
#登录
def login(request):
    login_main = _Login()
    if request.method == "POST":
        username_form = request.POST.get('Username')
        password_form = request.POST.get('Password')
        print('login——modth',username_form,password_form)
        #print('view-return',login_main._login(username_form, password_form))
        login_return = login_main._login(username_form, password_form)
        if login_return == 0 or login_return is None:
            return render_to_response('login.html',{'Message':'账号或密码错误，请重新输入'})
        elif login_return == 1:
            if username_form == "admin":
                return HttpResponseRedirect("/admin/adminIndex")
            request.session['LoginName_session'] = username_form
            request.session['Password_session'] = password_form
            #print('login_method',a,b)
            return HttpResponseRedirect('/index/')#个人信息
def user(request):
    return render_to_response('user.html')
#退出登录
def logout(request):
    del request.session['LoginName_session']
    return HttpResponseRedirect('/index/')
#注册
def register(request):
    Register_main = _Register()
    if request.method == "POST":
        username_form = request.POST.get('Username')
        password_form = request.POST.get('Password')
        email_form = request.POST.get('Email')
        print('register-modth',username_form,password_form,email_form)
        register_return = Register_main._register(username_form, password_form, email_form)
        if register_return == 0 or register_return == -1:
            return render_to_response('login.html',{'Message':'注册失败,错误码:'+str(register_return)})
        elif register_return == 1:
            return render_to_response('index.html', {'Message': '欢迎' + username_form + '加入奇点'})



#DACHUANGKAIYE
def index1(request):
    demo = 1
    meg_fill, page = area.Job_place().name_main()
    return render_to_response('showData_details_demo.html', {'demo': demo, 'Message': meg_fill, 'page': page})
def index2(request):
    detail_fill = policy._Policy(0, 0).policy_sql_id(17)
    return render_to_response('policy_demo.html', {'detail_fill': detail_fill})
def index3(request):
    pass
def index4(request):
    now_id = int(time.strftime('%d', time.localtime(time.time())))
    detail_fill = analysis._Analysis().analysis_sql_id(now_id)
    return render_to_response('analysis_allData_demo.html', {'detail_fill': detail_fill})
def index1_2(request):
    demo = 3
    meg_fill, page = area_map.Job_map().name_main()
    return render_to_response('showData_details_demo.html', {'demo': demo, 'Message': meg_fill, 'page': page})
def index1_3(request):
    demo = 2
    meg_fill, page = salary.Salary_place().name_main()
    return render_to_response('showData_details_demo.html', {'demo': demo, 'Message': meg_fill, 'page': page})
def index1_4(request):
    demo = 4
    meg_fill, page = undefined_4.Undefined_4().name_main()
    return render_to_response('showData_details_demo.html', {'demo': demo, 'Message': meg_fill, 'page': page})
def index5(request):
    now_id = int(time.strftime('%S', time.localtime(time.time())))
    detail_fill = policy._Policy(0, 0).policy_sql_id(now_id)
    return render_to_response('policy_demo.html', {'detail_fill': detail_fill})

def notfound(request):
    print("我被执行了！")
    return render_to_response('404.html')